/*
 * author      : 吴中勤
 * date        : 2019/3/13
 * description : TODO
 */

package org.wzq.learn.infrastructure.lib.trace;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CustomThreadContext {
    private static final Logger logger = LoggerFactory.getLogger(CustomThreadContext.class);

    public static class ContextAttributes {
        private String xRequestId;
        private String xTraceId;
        private Boolean isMock;

        @Override
        public String toString() {
            return "ContextAttributes {" +
                    "xRequestId='" + xRequestId + '\'' +
                    ", xTraceId='" + xTraceId + '\'' +
                    ", isMock=" + isMock +
                    '}';
        }

        ContextAttributes() {
        }

        public String getxRequestId() {
            return xRequestId;
        }

        public void setxRequestId(String xRequestId) {
            this.xRequestId = xRequestId;
        }

        public String getxTraceId() {
            return xTraceId;
        }

        public void setxTraceId(String xTraceId) {
            this.xTraceId = xTraceId;
        }

        public Boolean getMock() {
            return isMock;
        }

        public void setMock(Boolean mock) {
            isMock = mock;
        }
    }

    private static ThreadLocal<ContextAttributes> threadContext = new InheritableThreadLocal<ContextAttributes>() {
        protected ContextAttributes initialValue() {
            return new ContextAttributes();
        }
    };

    public static void bindContextAttributes(ContextAttributes contextAttributes) {
        threadContext.set(contextAttributes);
    }

    public static ContextAttributes currentAttributes() {
        return threadContext.get();
    }

    public static ContextAttributes clean() {
        ContextAttributes obj = currentAttributes();
        threadContext.remove();
        return obj;
    }

    public static String show() {
        if (null == currentAttributes()) return null;
        return currentAttributes().toString();
    }

    /* xRequestId  xRequestId  xRequestId */
    public static void setXRequestId(String id) {
        if (logger.isInfoEnabled()) logger.info("设置 xRequestId = " + id);
        threadContext.get().xRequestId = id;
    }

    public static String getXRequestId() {
        return threadContext.get().xRequestId;
    }
    /* xRequestId  xRequestId  xRequestId */


    /* xTraceId  xTraceId  xTraceId  xTraceId*/
    public static void setXTraceId(String id) {
        if (logger.isInfoEnabled()) logger.info("设置 xTraceId = " + id);
        threadContext.get().xTraceId = id;
    }

    public static String getXTraceId() {
        return threadContext.get().xTraceId;
    }
    /* xTraceId  xTraceId  xTraceId  xTraceId*/


    /* 是否真实流量 */
    public static Boolean isMock() {
        return threadContext.get().isMock;
    }

    public static void setMock(Boolean mock) {
        if (logger.isInfoEnabled()) logger.info("设置 isMock = " + mock);
        threadContext.get().isMock = mock;
    }


}
